package com.way.demo.threads;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author lipingwei
 * @create 2024-03-01 9:54
 * 线程池的使用
 *
 */
public class ThreadServiceImpl {

    //service 处理逻辑
    //线程池参数：
    private static final int EXECUTOR_SIZE = Runtime.getRuntime().availableProcessors()*2+1;

    private static ExecutorService executorSerice =
      new ThreadPoolExecutor(EXECUTOR_SIZE,EXECUTOR_SIZE,3L, TimeUnit.MINUTES,new LinkedBlockingDeque<>(30000));


    public String method_0(){
        //逻辑处理
        //TODO


        //线程池： 入参是资源类：执行run 方法中的代码
        executorSerice.execute(new TaskThread("p1","p2","p3"));

        return null;

    }

    //内部类：

    class TaskThread implements Runnable{

        private String p1;
        private String p2;
        private String p3;

        TaskThread(String p1,String p2,String p3){
            this.p1=p1;
            this.p2=p2;
            this.p3=p3;
        }

        @Override
        public void run() {
             method02("p1","p2","p3");
        }
    }

    private void method02(String str,String str2,String str3){

        //TODO 业务逻辑处理，是否考虑有并发的问题，redis 分布式锁
        //方法中的异常：记录日志 log

    }

}


